#include "StdAfx.h"
#include "HttpSession.h"

namespace HttpProtocol
{

CHttpSession::CHttpSession(LPCSTR pstrAgent/* = NULL*/,
	DWORD_PTR dwContext/* = 1*/,
	DWORD dwAccessType/* = PRE_CONFIG_INTERNET_ACCESS*/,
	LPCSTR pstrProxyName/* = NULL*/,
	LPCSTR pstrProxyBypass/* = NULL*/,
	DWORD dwFlags/* = 0*/)
{
	_ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);

	m_hSession = InternetOpenA(pstrAgent, dwAccessType,
		pstrProxyName, pstrProxyBypass, dwFlags);

	if (m_hSession == NULL)
	{
		throw CHttpException(GetLastError());
	}
}

CHttpSession::~CHttpSession(void)
{
}

CHttpConnect* CHttpSession::GetHttpConnection(LPCSTR pstrServer, DWORD dwFlags,
	INTERNET_PORT nPort/* = INTERNET_INVALID_PORT_NUMBER*/,
	LPCSTR pstrUserName/* = NULL*/, LPCSTR pstrPassword/* = NULL*/)
{
	return new CHttpConnect(this, pstrServer, dwFlags, nPort, pstrUserName, pstrPassword);
}

CHttpSession::operator HINTERNET() const
{
	return m_hSession;
}

BOOL CHttpSession::Release(void)
{
	return InternetCloseHandle(m_hSession);
}

}